package com.zwy.adbtest

import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader


object ShellUtils {
    /**
     * 执行Shell命令
     * @param command 要执行的命令
     * @return 命令执行的结果
     */
    fun executeShellCommand(command: String?): String {
        val output = StringBuilder()
        val process: Process
        try {
            process = Runtime.getRuntime().exec(command)
            val reader = BufferedReader(
                InputStreamReader(process.inputStream)
            )
            var line: String?
            while (reader.readLine().also { line = it } != null) {
                output.append(line).append("\n")
            }
            reader.close()
            process.waitFor()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return output.toString()
    }

    /**
     * 执行shell 命令， 命令中不必再带 adb shell
     * @param cmd
     * @return Sting  命令执行在控制台输出的结果
     */
    fun execByRuntime(cmd: String?): String? {
        var process: Process? = null
        var bufferedReader: BufferedReader? = null
        var inputStreamReader: InputStreamReader? = null
        return try {
            process = Runtime.getRuntime().exec(cmd)
            inputStreamReader = InputStreamReader(process.inputStream)
            bufferedReader = BufferedReader(inputStreamReader)
            var read: Int
            val buffer = CharArray(4096)
            val output = StringBuilder()
            while (bufferedReader.read(buffer).also { read = it } > 0) {
                output.appendRange(buffer, 0, read)
            }
            output.toString()
        } catch (e: Exception) {
            e.printStackTrace()
            null
        } finally {
            if (null != inputStreamReader) {
                try {
                    inputStreamReader.close()
                } catch (_: Throwable) {
                }
            }
            if (null != bufferedReader) {
                try {
                    bufferedReader.close()
                } catch (_: Throwable) {
                }
            }
            if (null != process) {
                try {
                    process.destroy()
                } catch (_: Throwable) {
                }
            }
        }
    }

    fun adbExec() {
        try {
            // 构建adb命令
            val command = "adb devices"

            // 执行命令
            val process = Runtime.getRuntime().exec(command)

            // 读取命令执行结果
            val reader = BufferedReader(InputStreamReader(process.inputStream))
            var line: String?
            while (reader.readLine().also { line = it } != null) {
                println(line)
            }

            // 等待命令执行完成
            process.waitFor()

            // 获取退出值
            val exitValue = process.exitValue()
            println("Exit value: $exitValue")
        } catch (e: IOException) {
            e.printStackTrace()
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }
    }
}